<?php
namespace app\merchant\controller;


use app\common\model\ClueProgress;
use think\Db;

class SubIndex extends BaseSub
{

    public function index(){
        $info = db('merchantSub')->where('id',session('subid'))->find();
        if($info['status']!=1){
            session('subid',null);
            cookie('subid',null);
            $this->redirect(url('Login/subLogin'));
        }
        $products = model('Product')->where('status',1)->column('name','id');

        $this->assign('info',$info);
        $this->assign('products',$products);
        return $this->fetch();
    }


    public function lists()
    {

        $clue_name = input('clue_name','');
        $phone = input('phone','');
        $source = input('source','0');
        $times = input('times','');
        $page = input('page','');
        $limit = input('limit','');
        $pro_id = input('pro_id','');
        $cityname = input('cityname','');

//        $map=[];
        $map['a.mer_id_sub'] = session('subid');
        if($clue_name != ''){
            $map['b.clue_name'] = ['like','%'.$clue_name.'%'];
        }
        if($phone != ''){
            $map['b.phone'] = $phone;
        }
        if($source != '0'){
            $map['b.source'] = $source;
        }
        if($pro_id!=''){
            $map['a.pro_id']=$pro_id;
        }

        //模糊查询城市
        if(!empty($cityname)){
            $cityIdArr=Db::name('city')->field('id,type,pid')->where('name','like',$cityname."%")->where('type',2)->find();
            if($cityIdArr){
                $map['a.city']=$cityIdArr['pid'].'-'.$cityIdArr['id'];
            }else{
                $map['a.city']=0;
            }
        }

        $config = model('SystemConfig')->getInfoByMap(['id'=>3]);
        if(!empty($config)&&$config['status']==1&&!empty($config['extra'])){
            $dayTime=$config['extra']*3600*24;
            $star_time=strtotime(date('Y-m-d'))-$dayTime;
            $map['a.create_time'] = ['>=',$star_time];
        }

        if($times != ''){
            $time_arr = explode(' - ',$times);
            $starTime=strtotime($time_arr[0]);
            if(empty($star_time)){
                $star_time=$starTime;
            }else{
                if($star_time<$starTime){
                    $star_time=$starTime;
                }
            }

            $map['a.create_time'] = ['between',[$star_time,strtotime($time_arr[1])]];
        }
        // 查询条件end


//        $feild='b.clue_name,b.age,b.phone';
        $lists = model('clue_log_sub_mer')->alias('a')
            ->field('a.id,b.clue_name,b.age,b.gender,a.phone,a.city,a.charge_type,a.price,a.status,a.create_time,b.remark,b.apply_limit,c.name pro_name,b.type,d.show_channel,a.show_city,b.house,b.car,b.public_funds')
            ->join('clue b','a.clue_id=b.id','LEFT')
            ->join('product c','c.id=a.pro_id','LEFT')
            ->join('account d','d.adv_id=a.adv_id','LEFT')
            ->where($map)->page($page,$limit)
            ->order('a.id desc')
            ->select();

        $total= model('clue_log_sub_mer')->alias('a')
            ->join('clue b','a.clue_id=b.id')
            ->where($map)->count();

        $channels = $this->getChannelArray();

        foreach ($lists as $k=>$v){
            if(!empty($v['show_city'])){
                $lists[$k]['city']=$v['show_city'];
            }else{
                if($v['city'] != ''){
                    $ids = explode('-',$v['city']);
                    $prov = $this->getCityName($ids[0]);
                    $city = $this->getCityName($ids[1]);
                    if($prov == $city){
                        $lists[$k]['city'] = $prov;
                    }else{
                        $lists[$k]['city'] = $prov.'-'.$city;
                    }
                }
            }
            $lists[$k]['gender']=config('clue.gender')[$v['gender']];
            $lists[$k]['charge_type'] = config('clue.charge_types')[$v['charge_type']];

            if(!empty($v['show_channel'])){
                $lists[$k]['channel_name'] = $v['show_channel'];
            }else{
                $lists[$k]['channel_name'] = $channels[$v['type']];
            }
            $content='';
            if(!empty($v['car'])){
                $content=$v['car'];
            }
            if(!empty($v['house'])){
                $content=$v['house'];
            }
            if(!empty($v['public_funds'])){
                $content='公积金：'.$v['car'];
            }
            $lists[$k]['content']=$content;

            if(empty($v['remark'])){
                $lists[$k]['remark_data']=[];
            }else{
                $remark=json_decode($v['remark'],true);
                if (is_array($remark)){
                    $remarkArr=[];
                    $keys=array_keys($remark);
                    $b=false;//默认索引
                    //判断关联数组还是索引数组
                    foreach($keys as $value){
                        if(!is_int($value)){
                            $b=true;
                            break;
                        }
                    }
                    //是关联数组
                    if($b){
                        foreach ($remark as $kk=>$vv){
                            if(strpos($kk, '城市') !== false){
                            }else{
                                $remarkArr[]=$kk.'：'.$vv;
                            }
                        }
                    }else{
                        foreach ($remark as $vv){
                            if($v['type']==1){
                                if(strpos($vv['name'], '城市') !== false){
                                }else{
                                    $remarkArr[]=$vv['name'].'：'.$vv['value'];
                                }
                            }else{
                                if(isset($vv['name'])&&isset($vv['value'])){
                                    if(strpos($vv['name'], '城市') !== false){
                                    }else{
                                        $remarkArr[]=$vv['name'].'：'.$vv['value'];
                                    }
                                }else{
                                    $remarkArr[]=json_encode($vv,JSON_UNESCAPED_UNICODE);
                                }
                            }
                        }
                    }
                }else{
                    $remarkArr=[];
                    $remarkArr[]=$v['remark'];

                }
                $lists[$k]['remark_data']=$remarkArr;
            }




            $lists[$k]['progress']=model('ClueProgress')->where('clue_log_id',$v['id'])->order('id desc')->select();
//            $lists[$k]['progress']=[];
//            foreach ($progress as $kk=>$vv){
//                $lists[$k]['progress'].=$kk.'.'.$vv.'<br>';
//            }

//            $remark=json_decode($v['remark'],true);
//            if (is_array($remark)){
//
//                $keys=array_keys($remark);
//                $b="";
//                foreach($keys as $v){
//                    if(!is_string($v)){
//                        $b=false;
//                    }else{
//                        $b=true;
//                        break;
//                    }
//                }
//                if($b){
//                    $remarkArr=[];
//                    foreach ($remark as $kk=>$vv){
//                        $remarkArr[]=$kk.'：'.$vv;
//                    }
//                }else{
//                    $remarkArr=[];
//                    foreach ($remark as $vv){
//                        $remarkArr[]=json_encode($vv,JSON_UNESCAPED_UNICODE);
//                    }
//                }
//            }else{
//                $remarkArr[]=$v['remark'];
//
//            }
//            $infos[$k]['remark_data']=$remarkArr;
//            if($v['push_time']){
//                $infos[$k]['push_time'] = date('Y-m-d H:i:s',$v['push_time']);
//            }
//
//            if($v['adv_id'] != ''){
//                $account = Db::name('account')->where('adv_id','=',$v['adv_id'])->find();
//                $infos[$k]['acc_name'] = $account['acc_name'];
//            }else{
//                $infos[$k]['acc_name'] = '手动添加的线索';
//            }
        }

        return json([
            'code'=>200,
            'data' => $lists,
            'total' => $total,
        ]);
    }

    //修改密码
    public function changePwd(){
        $pwd = input('pwd');
        if(empty($pwd)){
            return json(['code'=>400,'msg'=>'密码不能为空']);
        }
        $aid=session('subid');
        $isOK=db('merchant_sub')->where('id',$aid)->update(['password'=>$pwd,'update_time'=>time()]);
        if($isOK){
            return json(['code'=>200,'msg'=>'密码修改成功']);
        }else{
            return json(['code'=>400,'msg'=>'密码修改失败']);
        }
    }

    //修改手机
    public function changePhone(){
        $pwd = input('phone');
        if(empty($pwd)){
            return json(['code'=>400,'msg'=>'手机号不能为空']);
        }
        $aid=session('subid');
        $isOK=db('merchant_sub')->where('id',$aid)->update(['phone'=>$pwd,'update_time'=>time()]);
        if($isOK){
            return json(['code'=>200,'msg'=>'手机号修改成功']);
        }else{
            return json(['code'=>400,'msg'=>'手机号修改失败']);
        }
    }

//    //新增更进记录
//    public function saveProgress(){
//        $cid = input('cid');//线索id
//        $id = input('id');//记录id
//        $content = input('content');//记录id
//        if(empty($cid)){
//            return json(['code'=>400,'msg'=>'保存错误']);
//        }
//        if(empty($content)){
//            return json(['code'=>400,'msg'=>'记录内容不能为空']);
//        }
//        $minfo=db('clue_log')->where('id',$cid)->find();
//        $aid=session('subid');
//        if(empty($minfo)||$minfo['mer_id']!=$aid){
//            return json(['code'=>400,'msg'=>'非法请求']);
//        }
//
//        if(!empty($id)){
//            $log=db('clue_progress')->where('id',$id)->find();
//            if(empty($log)||$log['clue_log_id']!=$cid){
//                return json(['code'=>400,'msg'=>'非法请求']);
//            }
//            $save_data['id']=$id;
//        }
//        $save_data['clue_id']=$minfo['clue_id'];
//        $save_data['clue_log_id']=$cid;
//        $save_data['content']=$content;
//        $result =model('ClueProgress')->save($save_data);
//        if($result){
//            if(isset($id) && $id){
//                $msg = '修改成功';
//            }else{
//                $msg = '添加成功';
//            }
//            return json(['code'=>200,'msg'=>$msg]);
//        }else{
//            return json(['code'=>400,'msg'=>'修改失败']);
//        }
//
//    }
//
//    //删除跟进记录
//    public function delProgress(){
//        $id = input('id');//记录id
//        if(empty($id)){
//            return json(['code'=>400,'msg'=>'ID错误']);
//        }
//
//        $log=db('clue_progress')->where('id',$id)->find();
//        if(empty($log)){
//            return json(['code'=>400,'msg'=>'记录获取失败']);
//        }
//        $minfo=db('clue_log')->where('id',$log['clue_log_id'])->find();
//        $aid=session('subid');
//        if(empty($minfo)||$minfo['mer_id']!=$aid){
//            return json(['code'=>400,'msg'=>'非法请求']);
//        }
//
//        $result =ClueProgress::destroy($id);
//        if($result){
//
//            return json(['code'=>200,'msg'=>'删除成功']);
//        }else{
//            return json(['code'=>400,'msg'=>'删除失败']);
//        }
//    }

}
